package com.me.jingdongshop.ui.activity;

import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.me.common.base.BaseActivity;
import com.me.jingdongshop.R;
import com.me.jingdongshop.model.bean.Help;
import com.me.jingdongshop.model.bean.HelpDetail;
import com.me.jingdongshop.model.protocol.IHttpService;
import com.me.jingdongshop.presenter.TestFragmentPre;
import com.me.jingdongshop.ui.adapter.MyHelpAdaapter;
import com.me.jingdongshop.ui.adapter.MyHelpDetailAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/5/7.
 * 【我的模块】： 帮助中心和用户反馈界面
 */

public class MyHelpCenterActicity extends BaseActivity {
    private LinearLayout llFeedback;
    private ImageView imgBack;
    private TextView tvTitle;
    private EditText editText;
    private EditText editText2;
    private Button btnUsercenterCommit;
    private LinearLayout llHelpCenter;
    private ImageView imgHelpBack;
    private TextView tvHelpCenter;
    private ImageView imgAddAccount;
    private ListView lvHelp;
    private ListView lvHelpDetail;
    private ImageButton imgbHelpPhone;


    private TestFragmentPre mPresenter;
    private List<HelpDetail.HelpDetailListBean> mHelpDetailList;
    private ArrayList<String> mList;

    @Override
    public int getLayoutRes() {
        return R.layout.my_helpcenter_feedback;
    }

    @Override
    public void initView() {

        llFeedback = (LinearLayout) findView(R.id.ll_feedback);
        imgBack = (ImageView) findView(R.id.img_back);
        tvTitle = (TextView) findView(R.id.tv_title);
        editText = (EditText) findView(R.id.editText);
        editText2 = (EditText) findView(R.id.editText2);
        btnUsercenterCommit = (Button) findView(R.id.btn_usercenter_commit);
        llHelpCenter = (LinearLayout) findView(R.id.ll_help_center);
        imgHelpBack = (ImageView) findView(R.id.img_help_back);
        tvHelpCenter = (TextView) findView(R.id.tv_help_center);
        imgAddAccount = (ImageView) findView(R.id.img_add_account);
        lvHelp = (ListView) findView(R.id.lv_help);
        lvHelpDetail = (ListView) findView(R.id.lv_help_detail);
        imgbHelpPhone = (ImageButton) findView(R.id.imgb_help_phone);


    }

    @Override
    public void initListener() {
        imgHelpBack.setOnClickListener(this);
        imgAddAccount.setOnClickListener(this);
        imgBack.setOnClickListener(this);
    }

    @Override
    public void initData() {
        mPresenter = new TestFragmentPre(this);
        mPresenter.getHelpCenter();

    }

    @Override
    public void onClick(View v, int id) {
        switch (id) {
//          帮助中心
            case R.id.img_add_account:// 去到反馈界面
                llHelpCenter.setVisibility(View.GONE);
                llFeedback.setVisibility(View.VISIBLE);
                break;

//            反馈
            case R.id.img_back:// 返回帮助中心
                llFeedback.setVisibility(View.GONE);
                llHelpCenter.setVisibility(View.VISIBLE);
                break;

            case R.id.img_help_back:// 返回帐户中心
                showToast("返回帐号中心");
                finish();
                break;

            case R.id.btn_usercenter_commit:// 提交反馈
                if (editText.getText() != null && editText2.getText() != null) {
                    String detail = editText.getText().toString();
                    String phone = editText2.getText().toString();
                    showToast("下次别再点了-__-!");
                } else {
                    showToast("喂！你还有东西没填，请自觉@_@");
                }
                break;


        }

    }


    //    一开始没看接口写布局，然后才发现数据直接可以通过ListView展示，但是又需要重新改，算了，所以做得这么麻烦，日
    @Override
    public void onHttpSuccess(int reqType, Message msg) {

        if (reqType == IHttpService.HTTP_HELP) {
            Help help = (Help) msg.obj;
            if (help.getHelpList() != null) {
                List<Help.HelpListBean> helpList = help.getHelpList();
                lvHelp.setAdapter(new MyHelpAdaapter(this, helpList));
//                遍历HelpDetail的数据

                for (int i = 0; i < helpList.size(); i++) {
                    int id = helpList.get(i).getId();
                    mPresenter.getHelpDetail(id);
                    mList = new ArrayList<>();//在这里new对象确保每一次添加的都是不同数据
                }
            }
        }

        if (reqType == IHttpService.HTTP_HELP_DETAIL) {
            HelpDetail helpDetail = (HelpDetail) msg.obj;
            List<HelpDetail.HelpDetailListBean> helpDetailList = helpDetail.getHelpDetailList();
            mList.add(helpDetailList.get(0).getTitle());
            if (mList.size() == 4) {
                lvHelpDetail.setAdapter(new MyHelpDetailAdapter(this, mList));
            }


        }


    }


    @Override
    public void onHttpError(int reqType, String error) {
        showToast("网络出错了—_—!!");

    }


}
